home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
IRIX 6.5 Applications 1999 May
/
SGI IRIX 6.5 Applications 1999 May.iso
/
dist
/
nss_fasttrack.idb
/
var
/
netscape
/
fasttrack
/
js
/
samples
/
compiler
/
Calljsac.java.z
/
Calljsac.java
Wrap
Text File
|
1998-10-13
|
6KB
|
184 lines
package compiler;
import netscape.javascript.*;
//import netscape.server.serverenv.*;
import java.io.FileInputStream;
import java.io.DataInputStream;
import java.io.File;
/**
* A class to execute the LiveWire commandline compiler from Java
*/
public class Calljsac
{
/**************************************
* A convenience wrapper version *
* called from server-side JavaScript. *
***************************************/
public static String exec( String jsac, String path,
String filelist, String outfile,
boolean verbose )
{
int KNOWN_PARAMS = 4;
String[] params = new String[KNOWN_PARAMS+(verbose?1:0)];
// Start building a command line
// Use an include file that contains the list of source files to be compiled
params[0] = "-f";
params[1] = filelist;
// Designate name of .web file
params[2] = "-o";
params[3] = outfile;
// If verbose = true, add as a parameter
int i = KNOWN_PARAMS;
if( verbose )
params[i++] = "-v";
return exec( jsac, path, params, filelist );
}
public static String exec( String jsac, String path, String[] params, String filelist )
{
// Start a string that will hold all output from this routine and the compiler
String retString = "-1#";
// Generate a file name for the temporary file that will hold output from the compiler
String tempFilename = generateTempFilename();
// Build the command line, e.g. "jsac -v -o output.web ... "
String[] cmdArray = buildCmdArray( jsac, path, params, tempFilename );
// Create a handle to the temporary file that holds the output from the compiler
File tempFile = new File( path, tempFilename );
if( tempFile.exists() )
tempFile.delete();
try
{
/* For debugging only
int numArgs = cmdArray.length;
System.out.println("Command is " );
String temp = "";
for (int i=0; i<numArgs; i++) {
temp += cmdArray[i] + " ";
}
System.out.println( temp );
// -----------------------------
*/
// Launch the compiler
Process p = Runtime.getRuntime().exec( cmdArray );
p.waitFor();
// An exit value of 0 indicates success, anything else is an error
int retValue = p.exitValue();
// Begin building our return string
retString = retValue + "#";
p.destroy();
// If an error occurred, try and add informative error messages to the return string
// to help the user identify the problem.
if ( retValue != 0 )
{
retString += catchFileErrors(path, filelist, tempFile);
}
// Read the compiler output file, if any, and add to the return string
if( tempFile.exists() )
{
StringBuffer buf = null;
DataInputStream s = null;
try {
buf = new StringBuffer();
s = new DataInputStream(new FileInputStream(tempFile));
}
catch ( Exception e )
{
retString += "EXCEPTION in streaming\n";
retString += "Check file permissions for directory " + path + "\n";
}
if( null != s )
{
String str;
while( null != (str = s.readLine()) )
{
buf.append(str);
buf.append("\n");
}
}
s.close();
retString += buf;
}
}
catch( Exception e )
{
retString += "EXCEPTION: -1#\n";
retString += "Check your system path and be sure the path to the jsac compiler is correct.\n";
}
// Clean up our temporary compiler output file
if( tempFile.exists() )
tempFile.delete();
// Pass back results as a string
return retString;
}
private static String generateTempFilename()
{
return "__jsac.tmp"; // XXX this should be based on a random number..
} // END generateTempFilename
private static String catchFileErrors (String path, String filelist, File tempFile )
{
String retString = ""; // local variable to build result/error string
// Create a handle to filelist which contains list of files to be compiled
File filelistFileHandle = new File(path, filelist);
// Does the file indicated by 'filelist' exist?
if (!filelistFileHandle.exists()) {
retString += "Cannot find list of files to be compiled in file '" + filelist + "' \n";
retString += "in path " + path + ".\n";
retString += "Check to be sure the path is correct and that the file exists.\n";
} else if (!filelistFileHandle.canRead()) {
// Otherwise, do we have permission to read this file?
retString += "No permission to read file list '" + filelist + "'\n";
retString += "in path " + path + ".\n";
retString += "Check file and directory permissions.\n";
} else if ((tempFile.exists()) && (!tempFile.canWrite())) {
// Otherwise, do we have permission to write our temporary output file to the 'path'?
retString += "No permission to write compiler output errors to temporary file \n";
retString += "in path " + path + ".\n";
retString += "Check file and directory permissions.\n";
}
return retString;
} // END catchFileErrors
private static String[] buildCmdArray( String jsac, String path,
String[] params, String tempFilename )
{
String[] cmd = new String[params.length + 5];
// Build the start of the command line
cmd[0] = jsac;
cmd[1] = "-p";
cmd[2] = path;
cmd[3] = "-r";
cmd[4] = tempFilename;
// Add other parameters
for( int i = 0; i < params.length; i++ )
cmd[5+i] = params[i];
return cmd;
} // END buildCmdArray
} // END Class Calljsac